<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRoute } from 'vue-router'
import { timeFormat, timeFormat_2, toYuan } from '@/hooks/util'
import { message } from 'ant-design-vue'
import * as assetsOrderApi from '@/api/assetsOrder'
import { useUserStore } from '@/store/modules/user'

const userStore = useUserStore()
console.log('userStore=', userStore.userInfo)

const route = useRoute()

interface Pagination {
  page: number
  pageSize: number
  total: number
  current?: number
  onChange: Function
  showSizeChanger: boolean
  showQuickJumper?: boolean
}

const handlePageChange = (page: any, pageSize: any) => {
  // console.log(page, pageSize)
  pagination.value.pageSize = pageSize
  searchParams.value.pageSize = pageSize
}

const pagination = ref<Pagination>({
  page: 1,
  pageSize: 10,
  total: 0,
  onChange: handlePageChange,
  showSizeChanger: true,
  showQuickJumper: true
})
let loading = ref<boolean>(false)

interface SearchParams {
  currentPage: number
  pageSize: number
  [propName: string]: any
}
const searchParams = ref<SearchParams>({
  currentPage: pagination.value.page,
  pageSize: pagination.value.pageSize,
})

const dataSource = ref([])
const columns = [
  {
    title: '类型',
    key: 'id',
    width: 300,
  },
  {
    title: '金额',
    key: 'changeBalance',
    width: 300,
  },
  {
    title: '创建时间',
    key: 'createTime',
    width: 300,
    fixed: 'right',
  },
  // {
  //   title: '操作',
  //   key: 'action',
  //   width: 200,
  //   fixed: 'right',
  //   align: 'center',
  // },
]

// 翻页
const changePage = (e: any) => {
  pagination.value.current = e.current
  pagination.value.pageSize = e.pageSize
  searchParams.value.currentPage = pagination.value.current as number
  getList()
}

const getList = async () => {
  loading.value = true
  try {
    let { state, data, message: msg } = await assetsOrderApi.nbPage(searchParams.value)
    if (state === 200) {
      dataSource.value = data.list
      pagination.value.total = Number(data.totalCount)
    } else {
      message.error(msg)
    }
  } catch (error) {
    message.error('网络请求连接失败~')
  }
  loading.value = false
}

onMounted(() => {
  getList()
})

defineExpose({
  getList,
})

import type { SelectProps } from 'ant-design-vue'
const statusList = ref<SelectProps['options']>([
  {
    label: '收入',
    value: 1,
  },
  {
    label: '支出',
    value: 2,
  },
])

const formRef = ref()
interface FormState {
  changeType?: string
  [propName: string]: any
}
const formState = reactive<FormState>({})

// 查询
const search = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.validate().then(() => {
    // console.log('formState=', formState)

    if (formState.changeType) {
      searchParams.value.changeType = formState.changeType
    }

    getList()
  })
}

// 重置
const reset = () => {
  pagination.value.current = 1
  searchParams.value.currentPage = 1
  formRef.value.resetFields()

  formRef.value.resetFields()
  for (let key in searchParams.value) {
    if (key != 'currentPage' && key != 'pageSize')
      searchParams.value.hasOwnProperty(key) && delete searchParams.value[key]
  }

  getList()
}
</script>

<template>
  <div class="searchBox">
    <a-form ref="formRef" layout="inline" :model="formState" class="formClass">
      <a-form-item label="收入/支出" name="changeType">
        <a-select
          v-model:value="formState.changeType"
          placeholder="收入/支出"
          style="width: 150px"
          allowClear
        >
          <a-select-option v-for="(item, index) in statusList" :key="index" :value="item.value">
            {{ item.label }}
          </a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item>
        <a-button type="primary" style="border-radius: 6px" @click="search">搜索</a-button>
        <a-button @click="reset" style="border-radius: 6px">重置</a-button>
      </a-form-item>
    </a-form>
  </div>
  <a-table
    style="overflow-x: scroll"
    :columns="columns"
    :data-source="dataSource"
    :pagination="pagination"
    :loading="loading"
    @change="changePage"
    rowKey="id"
    :scroll="{ x: 100 }"
    size="small"
  >
    <template #emptyText>
      <a-empty description="暂无数据" />
    </template>

    <template #headerCell="{ column }"></template>

    <template #bodyCell="{ column, record }">
      <template v-if="column.key === 'id'">
        <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '80px' }">
          <a-descriptions-item label="id">{{ record.id }}</a-descriptions-item>
          <a-descriptions-item label="类型">
            <a-tag color="blue" v-if="record.changeType == 1">收入</a-tag>
            <a-tag color="red" v-if="record.changeType == 2">支出</a-tag>
          </a-descriptions-item>
        </a-descriptions>
      </template>

      <template v-else-if="column.key === 'changeBalance'">
        <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '80px' }">
          <a-descriptions-item label="金额">
            ￥{{ toYuan(record.changeBalance) }}
          </a-descriptions-item>
          <a-descriptions-item label="备注">
            <a-tooltip placement="top">
              <template #title>
                <span>{{ record.changeRemark }}</span>
              </template>
              <span class="hiddenText">{{ record.changeRemark }}</span>
            </a-tooltip>
          </a-descriptions-item>
        </a-descriptions>
      </template>

      <template v-else-if="column.key === 'createTime'">
        <a-descriptions bordered size="small" :column="1" :labelStyle="{ width: '100px' }">
          <a-descriptions-item label="创建时间">
            {{ timeFormat_2(record.createTime) }}
          </a-descriptions-item>
          <a-descriptions-item label="更新时间">
            {{ timeFormat_2(record.updateTime) }}
          </a-descriptions-item>
        </a-descriptions>
      </template>

      <template v-else-if="column.key === 'action'">
        <div style="width: 180px"></div>
      </template>
    </template>
  </a-table>
</template>

<style lang="less" scoped>
.hiddenText {
  display: inline-block;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  line-height: 16px;
}

.aTag-seatNo:nth-child(n + 2) {
  margin-top: 3px;
}

.formClass {
  :nth-child(1) {
    margin-bottom: 2px;
  }
}

.searchBox {
  margin-bottom: 10px;
  display: flex;

  input {
    width: 200px;
    margin-right: 10px;
  }

  button {
    margin-right: 10px;
  }
}
</style>
